rm(list = ls())
library(data.table)
library(plyr)
library(tidyr)
library(lfe)
library(stargazer)
library(xtable)
library(sandwich)
library(roll)
library(readxl)
library(readr)
library(zoo)
library(texreg)
library(DescTools) 

m <- data.table(read_xlsx("../Data/MasterData_Alt2024.xlsx"))

m[, retrecall_30day_rate := Winsorize(retrecall_30day_rate, quantile(retrecall_30day_rate, probs = c(0.05, 0.95), na.rm = T))]
m[, retrecall_1year_rate := Winsorize(retrecall_1year_rate, quantile(retrecall_1year_rate, probs = c(0.05, 0.95), na.rm = T))]

m[, expret30day_market_rate := Winsorize(expret30day_market_rate, quantile(expret30day_market_rate, probs = c(0.05, 0.95), na.rm = T))]
m[, expret1year_market_rate := Winsorize(expret1year_market_rate, quantile(expret1year_market_rate, probs = c(0.05, 0.95), na.rm = T))]

m[, expret30day_self_rate := Winsorize(expret30day_self_rate, quantile(expret30day_self_rate, probs = c(0.05, 0.95), na.rm = T))]
m[, expret1year_self_rate := Winsorize(expret1year_self_rate, quantile(expret1year_self_rate, probs = c(0.05, 0.95), na.rm = T))]

# waves are Baseline, Reverse, Switch
sample <- m[!is.na(expret30day_market_rate) &
    !is.na(expret30day_self_rate) &
    !is.na(retrecall_30day_rate) &
    !is.na(retrecall_1year_rate) &
    !is.na(expret1year_market_rate) &
    !is.na(expret1year_self_rate) &
    !is.na(q17) & !is.na(q19) & !is.na(retrecall_30day_rate)]

# Table A.12: Summary statistics for respondents’ demographics in RandomOrder
summstat <- sample[, .(
    male = 100 * mean(gender == "男"),
    age = median(age),
    college = 100 * mean(education %in% c("大学本科", "研究生及以上")),
    rich = 100 * mean(total_wealth %in% c(
        "10万元-50万元", "50万元-100万元",
        "100万元-500万元", "500万-1000万元", "1000万元及以上"
    ))
), by = .(wave)]
print.xtable(xtable(summstat), include.rownames = F)

# Table 10: Testing order effects in simulation
# Panel A
f1 <- felm(expret30day_market_rate ~ retrecall_30day_rate | gender + education + total_wealth + total_income +
             accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample[wave == 1])

f2 <- felm(expret30day_market_rate ~ retrecall_30day_rate | gender + education + total_wealth + total_income +
             accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample[wave == 2])

f3 <- felm(expret30day_market_rate ~ retrecall_30day_rate | gender + education + total_wealth + total_income +
             accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample[wave == 3])

stargazer(f1, f2, f3,
    align = TRUE, dep.var.labels.include = TRUE,
    covariate.labels = c("Recalled own return, 1M", "textit{Reverse}", "textit{Switch}", "Recalled own return, 1M * textit{Reverse}", "Recalled own return, 1M * textit{Switch}"),
    omit = c("Constant"), omit.stat = c("LL", "ser", "F", "rsq"), ord.intercepts = FALSE, no.space = TRUE,
    single.row = FALSE, column.sep.width = "0pt", digits = 2
)


# Panel B Others Outcome Variables
f4 <- felm(expret30day_market_rate ~ retrecall_30day_rate * factor(wave) | gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)

f5 <- felm(expret30day_self_rate ~ retrecall_30day_rate * factor(wave) | gender + education + total_wealth + total_income +
             accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)

f6 <- felm(q17 ~ retrecall_30day_rate * factor(wave) | gender + education + total_wealth + total_income +
             accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)

stargazer(f4, f5, f6, 
          align = TRUE, dep.var.labels.include = TRUE,
          omit = c("Constant"), omit.stat = c("LL", "ser", "F", "rsq"), ord.intercepts = FALSE, no.space = TRUE,
          single.row = FALSE, column.sep.width = "0pt", digits = 2
)

